#!/usr/bin/env python3
# -*- coding:utf-8 -*-
# __author__=LuLu
import yaml
import os
from selenium import webdriver
from selenium.webdriver.chrome.options import Options


class ReadYaml(object):
    yamlPath = '../testConfig/config.yaml'

    def read_yaml(self):
        try:
            from yaml import CLoader as Loader, CDumper as Dumper
        except ImportError:
            from yaml import Loader, Dumper
        try:
            with open(self.yamlPath, 'r', encoding='utf-8') as yaml_file:
                configData = yaml.load(yaml_file, Loader=yaml.FullLoader)
            return configData
        except IOError:
            print("---->读取配置文件出错，请检查是否存在config.yaml\n")

    # 获取域名

    def get_host(self):
        return self.read_yaml().get('host')

    # 获取用户信息

    def get_username(self):
        return self.read_yaml().get('userinfo').get('username')

    def get_password(self):
        return self.read_yaml().get('userinfo').get('password')

    # 获取chromeDriver路径
    def get_chrome_driver_path(self):
        return self.read_yaml().get('chromeDriverPath')

    # 获取是否静默运行
    def is_headless(self):
        option = webdriver.ChromeOptions()
        if self.read_yaml().get('isHeadLess') is True:
            option.add_argument('--window-size=1920x1080')
            option.add_argument('--start-maximized')
            option.add_argument('--headless')
            return option
        elif self.read_yaml().get('isHeadLess') is False:
            option.add_argument('--start-maximized')
            return option

    # mysql获取
    def get_mysql_connect_info(self):
        return self.read_yaml().get('mysqlinfo')
